<template>
	<view class="home-box">
		<view class="model">
			<u-cell-group :border="false">
				<u-cell title="线上交易" :titleStyle="titleStyle" @click="openOnlinePay">
					<view slot="value" class="right-name">点击申请开通</view>
				</u-cell>
			</u-cell-group>
		</view>
		<view class="check-model">
			<u-checkbox-group v-model="checkValue" @change="checkboxChange" shape="circle">
				<u-checkbox label="阅读并同意" name="1" labelSize="14">
				</u-checkbox>
			</u-checkbox-group>
			<view class="red" @click="openAgreement">《线上交易协议》</view>
		</view>
		<view class="model">
			<u-cell-group :border="false">
				<u-cell title="是否接单" :titleStyle="titleStyle" :border="false">
					<u-switch slot="value" v-model="setInfo.enable"  size="20"></u-switch>
				</u-cell>
			</u-cell-group>
		</view>
		<view class="model">
			<u-cell-group :border="false">
				<u-cell title="营业时间" :titleStyle="titleStyle">
                       <view class="label red" slot="value">{{businessHours}}小时</view>
					
				</u-cell>
				<u-cell title="开店时间" :isLink="true" :titleStyle="titleStyle" @click="openStar">
					<u--input placeholder="请输入营业时间" v-model="setInfo.openTime" slot="value" border="none"></u--input>
				</u-cell>
				<u-cell title="结束时间" :isLink="true" :titleStyle="titleStyle" @click="openEnd" :border="false">
					<u--input placeholder="请输入营业时间" v-model="setInfo.closedTime" slot="value" border="none"></u--input>
				</u-cell>
			</u-cell-group>
		</view>
		<view class="model">
			<u-cell-group :border="false">
				<u-cell title="配送距离" :titleStyle="titleStyle">
					<view class="right-box" slot="value"><u--input placeholder="请输入配送距离" v-model="setInfo.deliveryDistance" 
						border="none"></u--input>米</view>
					
				</u-cell>
				<u-cell title="自配送价格" :titleStyle="titleStyle">
					<u--input placeholder="请输入配送价格" v-model="setInfo.deliveryPrice" slot="value"
						border="none"></u--input>
				</u-cell>
				<u-cell title="最小下单金额" :titleStyle="titleStyle">
					<u--input placeholder="请输入最小下单金额" v-model="setInfo.limitPrice" slot="value"
						border="none"></u--input>
				</u-cell>
				<u-cell title="包装费" :titleStyle="titleStyle" :border="false">
					<u--input placeholder="请输入包装费" v-model="setInfo.packagePrice" slot="value" border="none"></u--input>
				</u-cell>
			</u-cell-group>
		</view>
		<view class="model" style="padding-bottom: 30rpx;">
			<u-cell-group :border="false">
				<u-cell title="邮寄配送" :titleStyle="titleStyle">
					<u-switch slot="value" v-model="setInfo.acceptPost"  size="20"></u-switch>
				</u-cell>
				<u-cell title="邮寄费" :titleStyle="titleStyle">
					<u--input placeholder="请输入邮寄费" v-model="setInfo.postPrice" slot="value" border="none"></u--input>
				</u-cell>
				<u-cell title="邮寄起送金额" :titleStyle="titleStyle">
					<u--input placeholder="请输入邮寄起送金额" v-model="setInfo.minOrderPrice" slot="value"
						border="none"></u--input>
				</u-cell>
				<u-cell title="满金额包邮" :titleStyle="titleStyle" :border="false">
					<u--input placeholder="请输入满金额包邮" v-model="setInfo.postMinOrderPrice" slot="value"
						border="none"></u--input>
				</u-cell>
			</u-cell-group>
			<view class="text-area-box"><u--textarea v-model="setInfo.noticeContext" placeholder="请输入内容"></u--textarea>
			</view>

		</view>
		<u-datetime-picker :show="showStar"  @confirm="confirmStar" mode="time"></u-datetime-picker>
		<u-datetime-picker :show="showEnd"  @confirm="confirmEnd" mode="time"></u-datetime-picker>
		<u-popup :show="showPop" @close="close" @open="open">
			<view class="tip">
				<view class="tip-content">确定开通线上支付吗？</view>
				<view class="btn-box">
					<view class="cancel">取消</view>
					<view class="cancel">确定</view>
				</view>

			</view>
		</u-popup>
		<view class="submitBtn" @click="saveSet">更新设置</view>
	</view>
</template>

<script>
	import {
		getStoreData,
		setStoreData,
		httpPost,
		httpGet
	} from "@/utils/http.js";

	export default {
		data() {
			return {
				titleStyle: {
					"font-size": "14px"
				},
				checkValue: [],
				isAgree: false,
				time: "",
				showStar: false,
				showEnd: false,
				value2: "",
				businessHours:"0",
				setInfo: {
					acceptPost: true, //是否开启邮寄配送
					addTime: "", //营业时间
					closedTime: "", //日常关店时间
					deliveryDistance: 0, //配送距离
					deliveryPrice: 0, //配送价格
					enable: true, //是否接单
					lgAcceptOrder: false, //是否开通O2O会员下单
					limitPrice: 0, //最低金额
					minOrderPrice: 0, //邮寄最低起送金额
					noticeContext: "", //备注提示内容
					openOnlinePay: 0, //开启在线支付
					openRiderDispatch: 0,
					openTime: "", //日常开店时间
					packagePrice: 0, //包装费
					postMinOrderPrice: 0, //邮寄满多少包邮金额
					postPrice: 0, //邮寄费
					storeId: 0,
					subStoreId: 0,
					updateTime: ""

				},
				showPop: false

			};
		},
		onShow() {

		},
		onLoad() {
                 this.getStoreSetInfo(); 
		},
		methods: {
			getStoreSetInfo(){
				const _this = this;
				httpGet("/api/o2ostoreconfig/info")
					.then((res) => {
				         _this.setInfo=res.data;
						 let arr1=[],arr2=[];
						 if(res.data.openTime!=""&&res.data.closedTime!=""){
							 arr1=res.data.openTime.split(":");
							 arr2=res.data.closedTime.split(":");
							 _this.businessHours=Number(arr2[0]-arr1[0]);
						 }
						
					})
					.catch((e) => {});
			},
			close() {
				this.showPop=false;
			},
			open(){
				this.showPop=true;
			},
			checkboxChange(data) {
				console.log(data)
				if (data.length != 0) {
					this.isAgree = true;
				} else {
					this.isAgree = false;
				}
			},
			
			openStar() {
				this.showStar = true;
			},
			openEnd() {
				this.showEnd = true;
			},
			openAgreement() {
				
			},
			confirmStar(time) {
				this.setInfo.openTime=time.value;
				//console.log(time,this.setInfo.openTime)
				//console.log(this.setInfo.openTime.split(":"))
				
				this.showStar = false;
			},
			confirmEnd(time) {
				this.setInfo.closedTime=time.value;
				let arr=[],arr2=[];
				arr=this.setInfo.openTime.split(":");
				arr2=this.setInfo.closedTime.split(":");
				if(Number(arr[0])>Number(arr2[0])){
					wx.showToast({
						icon: "none",
						title: "关店时间不能小于开店时间"
					});
				}else{
					//
					
					this.businessHours=Number(arr2[0])-Number(arr[0]);
					this.showEnd = false;
				}
				console.log(Number(arr[0]))
				
				
			},
			openOnlinePay() {
				const _this = this;
				if (this.isAgree) {
					httpGet("/api/o2ostoreconfig/applyOnlinePay")
						.then((res) => {
                           
						})
						.catch((e) => {});
				} else {
					wx.showToast({
						icon: "none",
						title: "请认真阅读协议并勾选同意"
					});
				}

			},
			saveSet(){
				const _this = this;
				
				httpPost("/api/o2ostoreconfig/update",this.setInfo)
					.then((res) => {
				         wx.showToast({
				         	icon: "none",
				         	title: "设置成功"
				         });
						 uni.navigateBack();
					})
					.catch((e) => {});
			}

		},
	};
</script>

<style lang="scss">
	.home-box {
		background-color: #F7F7F7;
		height: 100vh;
		padding-top: 20rpx;
	}

	.model {
		background-color: #fff;
		margin-bottom: 20rpx;

		.right-name {
			font-size: 28rpx;
		}

		.red {
			color: #f74f4f;
		}
		.right-box{
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
		}
	}

	.check-model {
		display: flex;
		flex-direction: row;
		justify-content: flex-end;
		margin-bottom: 20rpx;
		margin-right: 20rpx;

		.red {
			color: #f74f4f;
		}
	}

	.text-area-box {
		margin-top: 10rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;

	}

	.submitBtn {
		height: 80rpx;
		line-height: 80rpx;
		background: linear-gradient(to right, #28ECEA, #2695F5);
		margin-left: 20rpx;
		margin-right: 20rpx;
		color: #fff;
		text-align: center;
		width: 710rpx;
		border-radius: 20rpx;
	}
	.tip{
		width: 500rpx;
		padding: 40rpx;
	}
</style>